package com.katze.ssh.sftp;

import org.apache.sshd.sftp.client.SftpClient;

import java.io.Serializable;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class FileEntry implements Serializable {

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public static final ZoneId DEFAULT_ZONE_ID= ZoneId.systemDefault();

    private String name;

    private String extension;

    private String filename;

    private long size;

    private String modifyTime;

    private String path;

    private String fullPath;

    private boolean isDirectory;

    private Attributes attributes;

    public FileEntry() {}

    public FileEntry (String path, SftpClient.DirEntry dirEntry) {
        this(path, dirEntry.getFilename(), dirEntry.getAttributes());
    }

    public FileEntry (String path, String name, SftpClient.Attributes attributes) {
        this.path = path;
        this.filename = name;
        // name = /代表根目录，全路径则是/; path = /代表二级目录，全路径则是/xxx; 其他情况代表三级及以上目录，全路径则是/xxx/xxx
        this.fullPath = name.equals("/")? "/":path.equals("/")? path + name:path + "/" + name;
        this.attributes = new Attributes(attributes);
        this.size = attributes.getSize();
        this.isDirectory = attributes.isDirectory();
        this.modifyTime = attributes.getModifyTime().toInstant().atZone(DEFAULT_ZONE_ID).format(FORMATTER);
        resolveFilename(filename, isDirectory).accept(this::setNameWithExtension);
    }

    private void setNameWithExtension(String name, String extension) {
        this.name = name;
        this.extension = extension;
    }

    public String getName() {
        return name;
    }

    public String getExtension() {
        return extension;
    }

    public String getFilename() {
        return filename;
    }

    public long getSize() {
        return size;
    }

    public String getModifyTime() {
        return modifyTime;
    }

    public String getPath() {
        return path;
    }

    public String getFullPath() {
        return fullPath;
    }

    public boolean isDirectory() {
        return isDirectory;
    }

    public Attributes getAttributes() {
        return attributes;
    }

    public static Consumer<BiConsumer<String, String>> resolveFilename(String filename, boolean isDirectory) {
        int index = filename.lastIndexOf(".");
        if (index == -1 || index == 0 || isDirectory) {
            return fc -> fc.accept(filename, null);
        } else {
            return fc -> fc.accept(filename.substring(0, index), filename.substring(index + 1));
        }
    }

    @Override
    public String toString() {
        return filename + "{" +
                "isDirectory=" + isDirectory +
                ", size=" + size +
                ", modifyTime='" + modifyTime + '\'' +
                ", path='" + path + '\'' +
                ", fullPath='" + fullPath + '\'' +
                ", attributes=" + attributes +
                '}';
    }
}
